package com.hpx.javaeye.screen;

import java.io.IOException;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.FlowFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.jaxp.DOMInternalRepresentation;

import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.hpx.chinastore.resource.JavaEyeResource;
import com.hpx.javaeye.business.RenJianBusiness;
import com.hpx.javaeye.util.Authentication;
import com.hpx.javaeye.util.BlackberryLogger;

public class ListScreen extends MainScreen implements JavaEyeResource {
	private static ResourceBundle _resource = ResourceBundle.getBundle(
			BUNDLE_ID, BUNDLE_NAME);
	private BlackberryLogger logger = BlackberryLogger.getInstance();

	public ListScreen() {
		// uiManage();
		test();
	}

	private void test() {
		String target = _resource.getString(PUBLIC_TIMELINE);
		String paras = "<root></root>";
		try {
			new RenJianBusiness(){
				protected void process(byte ch) {
					Dialog.alert("good" + ch);
				}
				protected void process(byte[] ch) {
					Dialog.alert("good" + new String(ch));
				}
				protected void processType(String type) {
					Dialog.alert(type);
				}
			}.processRequest(target, paras); 

			DOMInternalRepresentation dom = new DOMInternalRepresentation();
			DocumentFragment df = dom.parseFragment(paras);
			NodeList list = df.getChildNodes();
			if (0 != list.getLength())
				list = list.item(0).getChildNodes();
			for (int i = 0; i < list.getLength(); i++) {
				Node n = list.item(i);
				FlowFieldManager flow = new FlowFieldManager();
				flow.add(new LabelField(n.getNodeName()));
				flow.add(new LabelField(n.getFirstChild().getNodeValue()));
				add(flow);
			}
		} catch (SAXException e) {
			Dialog.alert(e.getMessage());
		} catch (IOException e) {
			Dialog.alert(e.getMessage());
		}
	}

	private void uiManage() {
		/*
		 * String fieldOne = "Mark Guo"; String fieldTwo = "Jason HuangJason " +
		 * "HuangJason HuangJason HuangJason " + "HuangJason HuangJason
		 * HuangJason HuangJason " + "HuangJason HuangJason HuangJason
		 * HuangJason Huang";
		 */
		String url = _resource.getString(PUBLIC_TIMELINE/* TEST_URL */);
		String username = "huangpengxiao";
		String password = "830304";
		Authentication ac = new Authentication() {
			public void process(byte[] b) {
				System.out.println(new String(b));

				// User user = new User().extractFromJson(new String(b));
				// System.out.println(user);
				//
				// JavaEye j = (JavaEye) UiApplication.getUiApplication();
				//
				// j.setUser(user);
				//
				// ListScreen screen = new ListScreen();
				//
				// j.pushScreen(screen);

			}

			public void process(byte b) {
			}

			public void processType(String type) {
			}
		};

		ac.passHttpBasic(url, username, password);
	}
	// Field[][] fs = new Field[2][2];
	// FlowFieldManager flow = new FlowFieldManager();
	// flow.add(new RichTextField(fieldOne, DrawStyle.ELLIPSIS));
	// flow.add(new LabelField(" "));
	// flow.add(new LabelField(new Date().toString()));
	// FlowFieldManager flow1 = new FlowFieldManager();
	// flow1.add(new RichTextField(fieldOne, DrawStyle.ELLIPSIS));
	// flow1.add(new LabelField(" "));
	// flow1.add(new LabelField(new Date().toString()));
	// fs[0][0] = new BitmapField(Bitmap.getBitmapResource("qqshow.jpg"));
	// fs[0][1] = flow;
	// fs[1][0] = new BitmapField(Bitmap.getBitmapResource("Earth.bmp"));
	// fs[1][1] = flow1;

	// ComplexList cl = new ComplexList(fs);
	// add(cl);

}
